package com.litidea.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateTimeUtils {

    /**
     * 缺省的日期显示格式： yyyy-MM-dd
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 缺省的日期时间显示格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 缺省时间显示格式：HH:mm:ss
     */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    /**
     * 增加确认格式
     */
    public static final String DEFAULT_DATE_FORMAT2 = "yyyyMMdd";

    /**
     * 星期几的时间与
     */
    private static Map <String,String>weekDayStr = new HashMap<String,String>();

    //公共初始化匿名块
    static {
    	weekDayStr.put("W1", "星期天");
    	weekDayStr.put("W2", "星期一");
    	weekDayStr.put("W3", "星期二");
    	weekDayStr.put("W4", "星期三");
    	weekDayStr.put("W5", "星期四");
    	weekDayStr.put("W6", "星期五");
    	weekDayStr.put("W7", "星期六");
    }

    /**
     *
     * @return
     */
    public static Date getNow() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 获取一个较为模糊匹配的日期格式
     * @param date
     * @return
     */
    public static Date getSomeDate(String date) {
        if(date == null) return null;
        int length = date.length();
        if(length == 10) {
            if(date.contains("-")) {
                return getDateByString(date , DEFAULT_DATE_FORMAT);
            }else {
                return getDateByString(date , "MM/dd/yyyy");
            }
        }else if(length > 15){
            if(date.contains("-")) {
                return getDateByString(date , DEFAULT_DATETIME_FORMAT);
            }else {
                return getDateByString(date , "MM/dd/yyyy HH:mm:ss");
            }
        }else if(length == 8) {
            if(date.contains(":")) {
                return getDateByString(date , DEFAULT_TIME_FORMAT);
            }else {
                return getDateByString(date , DEFAULT_DATE_FORMAT2);
            }
        }else {
            return getDateByString(date , DEFAULT_DATETIME_FORMAT);
        }
    }

    /**
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateTime(Date date, String pattern) {
        if (null == pattern || "".equals(pattern)) {
            pattern = DEFAULT_DATETIME_FORMAT;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     *
     * @param pattern
     * @return
     */
    public static String getDateTime(String pattern) {
        Date datetime = Calendar.getInstance().getTime();
        return getDateTime(datetime, pattern);
    }

    /**
     *
     * @return
     */
    public static String getNowYear() {
        return getDateTime("yyyy");
    }

    /**
     *
     * @return
     */
    public static int getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     *
     * @return
     */
    public static String getDate() {
        return getDateTime(DEFAULT_DATE_FORMAT);
    }

    /**
     *
     * @return
     */
    public static String getDateTime() {
        return getDateTime(DEFAULT_DATETIME_FORMAT);
    }

    /**
     *
     * @return
     */
    public static int getCurrentMonth() {
        // 用get得到的月份数比实际的小1，需要加上
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     *
     * @return
     */
    public static int getCurrentDay() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    /**
     * 获取年中的天数
     * @param date
     * @return
     */
    public static int getDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取天内的小时数
     * @param date
     * @return
     */
    public static int getHourOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取年当中的星期数目
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取年中的月份
     * @param date
     * @return
     */
    public static int getMonthOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH);
    }

    /**
     *
     * @param date
     * @param amount
     * @param field
     * @return
     */
    private static Date add(Date date, int amount, int field) {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.add(field, amount);

        return calendar.getTime();
    }

    /**
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        return add(date, days, Calendar.DATE);
    }

    /**
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonths(Date date, int months) {
        return add(date, months, Calendar.MONTH);
    }

    /**
     *
     * @param date
     * @param years
     * @return
     */
    public static Date addYears(Date date, int years) {
        return add(date, years, Calendar.YEAR);
    }

    /**
     *
     * @param one
     * @param two
     * @return
     */
    public static long diffDays(Date one, Date two) {
        return (one.getTime() - two.getTime()) / (24 * 60 * 60 * 1000);
    }

    /**
     *
     * @param one
     * @param two
     * @return
     */
    public static int diffMonths(Date one, Date two) {
        Calendar calendar = Calendar.getInstance();
        // 得到第一个日期的年分和月份数
        calendar.setTime(one);
        int yearOne = calendar.get(Calendar.YEAR);
        int monthOne = calendar.get(Calendar.MONDAY);
        // 得到第二个日期的年份和月份
        calendar.setTime(two);
        int yearTwo = calendar.get(Calendar.YEAR);
        int monthTwo = calendar.get(Calendar.MONDAY);

        return (yearOne - yearTwo) * 12 + (monthOne - monthTwo);
    }

    /**
     *
     * @param year
     * @return
     */
    public static String[] getYearBeginEnd(int year) {
    	String returnVal[] = new String[2];
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(year, 0, 1);
    	returnVal[0] = getDateTime(calendar.getTime(),DEFAULT_DATE_FORMAT);
    	calendar.set(year, 11, 1);
    	calendar.add(Calendar.DATE, -1);
    	returnVal[1] = getDateTime(calendar.getTime(),DEFAULT_DATE_FORMAT);
    	return returnVal;
    }

    /**
     *
     * @param year
     * @return
     */
    public static List<Map<String,String>> getYearMonths(int year) {
    	List <Map<String,String>>list = new ArrayList<Map<String,String>>();
    	for(int i = 1 ; i < 13 ; i ++) {
    		Map<String,String>map = new HashMap<String,String>();
    		map.put("dateinfo", year + "-" + (i < 10 ? "0"+i : String.valueOf(i)));
    		map.put("dayValue", "0.00");
    		map.put("dispname", year + "年" + i + "月");
    		list.add(map);
    	}
    	return list;
    }

    /**
     * 获取指定日期的当前月的最后一天
     * @param date
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 获取指定年、月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static Date getMonthLastDay(int year,int month) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(year, month, 1);
    	calendar.add(Calendar.DATE, -1);
    	return calendar.getTime();
    }

    /**
     * 获取指定年、月的起始时间
     * @param year
     * @param month
     * @return
     */
    public static String [] getMonthBeginEnd(int year,int month) {
    	String returnVal[] = new String[2];
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(year, month, 1);
    	calendar.add(Calendar.DATE, -1);
    	returnVal[1] = getDateTime(calendar.getTime(),DEFAULT_DATE_FORMAT);
    	calendar.set(year, month-1, 1);
    	returnVal[0] = getDateTime(calendar.getTime(),DEFAULT_DATE_FORMAT);
    	return returnVal;
    }

    /**
     * 获取指定日期当前月份的起始时间
     * @param date
     * @return
     */
    public static List<Map<String,String>> getMonthBeginAndEnd(Date date) {
    	List <Map<String,String>>info = new ArrayList<Map<String,String>>(32);
    	Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);
        calendar.add(Calendar.DATE, -1);
        int year = calendar.get(Calendar.YEAR);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH) + 1;
        for(int i = 1 ; i <= dayOfMonth ; i++) {
        	Map <String,String>map = new HashMap<String,String>();
        	map.put("dateinfo", year + "-" + (month<10?"0"+month:String.valueOf(month)) + "-" +(i < 10 ? "0" + i : String.valueOf(i)));
        	map.put("dayValue", "0.00");
        	map.put("dispname", month + "月" + i + "日");
        	info.add(map);
        }
        return info;
    }

    /**
     * 获取当前月份最后一天
     * @return
     */
    public static Date getMonthLastDay() {
        return getMonthLastDay(getNow());
    }

    /**
     * 获取当前星期几的编号
     * @return
     */
    public static int getNowWeek(Date objDate) {
        Calendar objCalendarDate = Calendar.getInstance();
        objCalendarDate.setTime(objDate);
        return objCalendarDate.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取对应星期几的中文
     * @param objDate
     * @return
     */
    public static String getWeekDay(Date objDate) {
    	Calendar objCalendarDate = Calendar.getInstance();
        objCalendarDate.setTime(objDate);
        String day = String.valueOf(objCalendarDate.get(Calendar.DAY_OF_WEEK));
        return (String)weekDayStr.get(day);
    }

    /**
     * 或者指定日期当前周的最后一天
     * @param date
     * @return
     */
    public static String[] getWeekBeginEnd(Date date) {
    	String returnVal[] = new String[2];
    	Calendar objCalendarDate = Calendar.getInstance();
    	objCalendarDate.setTime(date);
    	int day = objCalendarDate.get(Calendar.DAY_OF_WEEK);
    	day = day == 1 ? 8:day;
    	objCalendarDate.add(Calendar.DATE, (8-day));
    	returnVal[1] = getDateTime(objCalendarDate.getTime(),DEFAULT_DATE_FORMAT);
    	objCalendarDate.add(Calendar.DATE, -6);
    	returnVal[0] = getDateTime(objCalendarDate.getTime(),DEFAULT_DATE_FORMAT);
    	return returnVal;
    }

    /**
     * 获取一周的日期列表信息
     * @param date
     * @return
     */
    public static List<Map<String,String>> getWeekLong(Date date) {
    	List <Map<String,String>>list = new ArrayList<Map<String,String>>();
    	Calendar objCalendarDate = Calendar.getInstance();
    	objCalendarDate.setTime(date);
    	int day = objCalendarDate.get(Calendar.DAY_OF_WEEK);
    	day = day == 1 ? 8:day;
    	objCalendarDate.add(Calendar.DATE, (2 - day - 1));//设置上周周日的时间
    	for(int i = 2 ; i < 9 ; i ++) {
    		objCalendarDate.add(Calendar.DATE, 1);
    		Map <String,String>temp = new HashMap<String,String>();
    		String dateStr = getDateTime(objCalendarDate.getTime(),DEFAULT_DATE_FORMAT);
    		temp.put("dateinfo", dateStr);
    		temp.put("dayValue", "0.00");
    		temp.put("dispname", weekDayStr.get("W"+(i == 8 ? 1 : i)) + "(" + dateStr + ")");
    		list.add(temp);
    	}
    	return list;
    }

    /**
     * 获取对应周的第一天
     * @param year
     * @param month
     * @param week
     * @return
     */
    public static Date getWeekOneDate(int year,int month,int week) {
    	Calendar objCalendarDate = Calendar.getInstance();
    	objCalendarDate.set(year, month - 1, 1);//找到该月的1号，肯定为第一周
    	if(objCalendarDate.get(Calendar.DAY_OF_WEEK) == 1) {
    		objCalendarDate.add(Calendar.DATE, 1);
    	}
    	objCalendarDate.add(Calendar.DATE, (week - 1) * 7);
    	return objCalendarDate.getTime();
    }



    /**
     * 获取为月份中的第几周
     * @param objDate
     * @return
     */
    public static int getWeekOfMonth(Date objDate) {
    	Calendar objCalendarDate = Calendar.getInstance();
        objCalendarDate.setTime(objDate);
        return objCalendarDate.get(Calendar.WEEK_OF_MONTH);
    }

    /**
     * 将字日期符串，转换为util.Date
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date getDateByString(String date,String pattern) {
        if(date == null) return null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		    return dateFormat.parse(date);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
